import {ToolUtils} from "../../util/ToolUtils";
import {PlayCardUI} from "../component/PlayCardUI";
import {GameAction} from "./GameAction";

const {ccclass, property} = cc._decorator;

@ccclass
export class PlayerUI extends cc.Component {

    @property(cc.Label)
    nameLabel:cc.Label = null;
    @property(cc.Label)
    bgoldLabel:cc.Label = null;
    @property(cc.Sprite)
    headSp:cc.Sprite = null;
    @property(cc.Sprite)
    rankSp:cc.Sprite = null;
    @property(cc.Node)
    offlineNode:cc.Node = null;
    @property(cc.Node)
    clockNode:cc.Node = null;
    @property(cc.Node)
    readyNode:cc.Node = null;
    @property(cc.Node)
    notOutNode:cc.Node = null;
    @property(PlayCardUI)
    handCards:PlayCardUI = null;
    @property(PlayCardUI)
    outCards:PlayCardUI = null;
    @property
    localSeat: number = 0;

    onDestroy()
    {
        GameAction.removeCountdown(this.localSeat);
    }

    init()
    {
        this.showClock(false);
        this.showHandCard(false);
        this.showOutCard(false);
        this.showReady(false);
        this.showNotOut(false);
        this.showRank(false);
        this.showOffline(false);
    }

    setVisible(bShow:boolean)
    {
        this.node.active = bShow;

    }

    showClock(bShow:boolean,time?:number,remain?:number){
        GameAction.removeCountdown(this.localSeat);
        this.clockNode.active = bShow;
        if(time){
            GameAction.countdown(this.localSeat,this.clockNode,time,remain);
        }
    }

    showOutCard(bShow:boolean,cards?:Array<number>,action?:boolean){
        this.outCards.setVisible(bShow);
        cards = cards!= null?cards:[];
        if(action){
            //TODO
            this.outCards.setCards(cards);
        }else{
            this.outCards.setCards(cards);
        }
    }

    showHandCard(bShow:boolean,cards?:Array<number>,action?:boolean){
        this.handCards.setVisible(bShow);
        cards = cards!= null?cards:[];
        if(action){
            this.handCards.takeFirstCards(cards);
        }else{
            this.handCards.setCards(cards);
        }
    }

    showName(bShow:boolean,name?:string){
        this.nameLabel.node.active = bShow;
        if(name != null) ToolUtils.nameToShort(name,this.nameLabel,75);
    }

    showBodyGold(bShow:boolean,score?){
        this.bgoldLabel.node.active = bShow;
        if(score!=null) this.bgoldLabel.string = score;
    }

    showHead(bShow:boolean,headimg?:string){
        this.headSp.node.active = bShow;
        ToolUtils.loadNetImage(headimg,this.headSp);
    }

    showRank(bShow:boolean,rank?:number){
        this.rankSp.node.active = bShow;
        if(rank!=null){
            //this.rankSp.spriteFrame = new cc.SpriteFrame(cc.url.raw("game/shuangkou/rank_"+rank+".png"));
            var self = this
            cc.resources.load("game/shuangkou/rank_"+rank, cc.SpriteFrame, function (err, spriteFrame:cc.SpriteFrame) {
                self.rankSp.spriteFrame = spriteFrame;
            });
        }
    }

    showReady(bShow:boolean){
        this.readyNode.active = bShow;
    }

    showNotOut(bShow:boolean){
        this.notOutNode.active = bShow;
    }

    showOffline(bShow:boolean){
        this.offlineNode.active = bShow;
    }
}
